// https://leetcode.cn/problems/edit-distance/
class Solution {
    public int minDistance(String s1, String s2) {
        int n = s1.length(), m = s2.length();
        char[] cs1 = s1.toCharArray(), cs2 = s2.toCharArray();
        // dp[i][j] 将 s1[0..n] 变成 s2[0..m] 需要的最少操作数
        int[][] dp = new int[n + 1][m + 1];
        for (int i = 0; i <= n; i++) dp[i][0] = i;
        for (int i = 0; i <= m; i++) dp[0][i] = i;

        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                // 不需要进行操作
                if (cs1[i - 1] == cs2[j - 1])
                    dp[i][j] = dp[i - 1][j - 1];
                // 需要进行操作: 取 添加, 删除, 修改 的最小值
                else 
                    dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
        return dp[n][m];
    }
}